package com.zjeav.lingjiao.base.receiver;

import android.annotation.TargetApi;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;

import com.zjeav.lingjiao.R;
import com.zjeav.lingjiao.base.listener.CallBackTypeInteraction;
import com.zjeav.lingjiao.ui.music.MusicActivity;

import java.util.Timer;

/**
 * Created by kekezhou on 2018-11-01.
 */
public class MusicPlayerReceiver extends BroadcastReceiver {
    boolean isRegisterReceiver=false;
    public static final String UPDATE_ACTION = "com.flashmusic.action.UPDATE_ACTION";  //更新动作
    public static final String CTL_ACTION = "com.flashmusic.action.CTL_ACTION";        //控制动作
    public static final String MUSIC_CURRENT = "com.flashmusic.action.MUSIC_CURRENT";  //音乐当前时间改变动作
    public static final String MUSIC_DURATION = "com.flashmusic.action.MUSIC_DURATION";//音乐播放长度改变动作
    public static final String MUSIC_PLAYING = "com.flashmusic.action.MUSIC_PLAYING";  //音乐正在播放动作
    public static final String REPEAT_ACTION = "com.flashmusic.action.REPEAT_ACTION";  //音乐重复播放动作
    public static final String SHUFFLE_ACTION = "com.flashmusic.action.RANDOM_ACTION";//音乐随机播放动作
    public static final String PLAY_STATUE = "com.flashmusic.action.PLAY_STATUE";      //更新播放状态
    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        brInteraction1.callBackType(action,intent);
        //当前时间更新
//        if (action.equals(MUSIC_CURRENT)) {
//            currentTime = intent.getIntExtra("currentTime", -1);
//            seekBar.setProgress(currentTime);
//            MusicCurrentTime.setText(formatDuring(currentTime));
//
////                seekBar.setMax(duration);
//
//        } else if (action.equals(MUSIC_DURATION)) {
//            if(mTimer == null){
//                mTimer = new Timer();
//                mTask = new MusicActivity.LrcTask();
//                mTimer.scheduleAtFixedRate(mTask, 0, mPalyTimerDuration);
//            }
//            duration = intent.getIntExtra("duration", -1);
//            //总时间更新
//            MusicTime.setText(formatDuring(duration));
//            seekBar.setMax(duration);
//
//        } else if (action.equals(UPDATE_ACTION)) {
//            position = intent.getIntExtra("position", -1);
//            String url = mp3Infos.get(position).getUrl();
//
//            MusicTime.setText(formatDuring(mp3Infos.get(position).getDuration()));
////                currentTime = 0;
//            seekBar.setMax(duration);
//
//        } else if (action.equals(PLAY_STATUE)) {
//            boolean playstatue = intent.getBooleanExtra("playstatue", true);
//            if (playstatue) {
//                MusicPlay.setImageDrawable(getResources().getDrawable(R.mipmap.paly_pause, null));
//                isPlaying = true;
//            } else {
//                MusicPlay.setImageDrawable(getResources().getDrawable(R.mipmap.paly_play, null));
//                isPlaying = false;
//                stopLrcPlay();
//            }
//        }

    }
    static private CallBackTypeInteraction brInteraction1;

    public  void setBRInteractionListener(CallBackTypeInteraction brInteraction) {
        brInteraction1 = brInteraction;
    }
    /**
     * 广播注册
     *
     * @param mContext 上下文对象
     */
    public void registerScreenActionReceiver(Context mContext) {
        if (!isRegisterReceiver) {
            isRegisterReceiver = true;
            //注册广播
//
            IntentFilter filter = new IntentFilter();
            filter.addAction(UPDATE_ACTION);
            filter.addAction(MUSIC_CURRENT);
            filter.addAction(MUSIC_DURATION);
            filter.addAction(PLAY_STATUE);

            mContext.registerReceiver(MusicPlayerReceiver.this, filter);
        }
    }
    /**
     * 广播注销
     *
     * @param mContext 上下文对象
     */
    public void unRegisterScreenActionReceiver(Context mContext) {
        if (isRegisterReceiver) {
            isRegisterReceiver = false;
//                Log.i(TAG, "注销屏幕解锁、加锁广播接收者...");
            mContext.unregisterReceiver(MusicPlayerReceiver.this);
        }
    }

}

